package model

import (
	"time"

	"gorm.io/gorm"
)

type SendChat struct {
	gorm.Model
	GroupID  int64  `gorm:"index;comment:群号"`
	UserID   int64  `gorm:"index;comment:Q号"`
	Username string `gorm:"comment:Q昵称"`
	Message  string `gorm:"type:text;comment:信息"`
	AddTime  time.Time
}

type CountMessage struct {
	UserID   int64
	Username string
	Message  string
	Count    int64
}

func getSendChatModel() (tx *gorm.DB) {
	return mysqlDb.Model(&SendChat{})
}

func ChatAddLog(data *SendChat) {
	getSendChatModel().Create(&data)
}

func GetSendChatDataByUserID(userID int64) (cl []CountMessage) {
	getSendChatModel().Select("message, count(message) count").Where(&SendChat{
		UserID: userID,
	}).Group("message").Order("COUNT(message) DESC").Limit(15).Find(&cl)
	return
}

func DelSendChatByUserID(userID int64) (cl *SendChat) {
	getSendChatModel().Where(&SendChat{
		UserID: userID,
	}).Delete(&cl)
	return
}

func GetSendChatDataByGroup() (cl []CountMessage) {
	getSendChatModel().Select("user_id,username,message,count(message) count").Group("user_id").Order("COUNT(message) DESC").Find(&cl)
	return
}
